from flask import Flask, abort, render_template, request, make_response, session, Markup
from datetime import timedelta
from flask_script import Manager
from views import login_v, signin_v, user_v, blog_v

app = Flask(__name__)
app.config['SECRET_KEY'] = '123456'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=30)


@app.route('/', methods=['post', 'get'])  # 主页
def index_page():
    if request.args.get("del") == "del":
        session.clear()
    tag = '<a href="/login" id="login" >登录</a>&nbsp;|&nbsp;<a href="/signin" id="signin">注册</a>'
    markup_tag = Markup(tag)
    data = {'username': "",
            "login_status": markup_tag}
    if session.get('username'):
        data['username'] = session.get('username')
        tag = '<a href="/?del=' + 'del' + '" id="login" >注销</a>'
        data["login_status"] = Markup(tag)
    return render_template('index.html', data=data)


if __name__ == '__main__':
    app.register_blueprint(login_v.blu)
    app.register_blueprint(signin_v.blu)
    app.register_blueprint(user_v.blu)
    app.register_blueprint(blog_v.blu)
    manger = Manager(app)
    manger.run()
